<template>
  <el-dialog :visible.sync="dialogVisible" :append-to-body="true" title="消息"
             width="50%" @open="open">
    <el-form ref="form" :model="message" label-width="100px">
      <el-row :gutter="10">
        <el-col :span="24">
          <el-form-item prop="messageTitle" label="消息标题">
            <el-input :readonly="readOnly" v-model="message['messageTitle']"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item prop="messageType" label="消息类型">
            <el-input :readonly="readOnly" v-model="message['messageType']"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item prop="createUserName" label="发送人">
            <el-input :readonly="readOnly" v-model="message['createUserName']"/>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item prop="messageContent" label="消息内容">
            <el-input type="textarea" :readonly="readOnly" v-model="message['messageContent']"/>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span slot="footer">
        <el-button size="small" @click="closeDialog">取消</el-button>
        <el-button size="small" @click="closeDialog">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { consumeMessageByIds } from '@/api/ApiMessage'
import BusinessConstants from '@/constants/BusinessConstants'

export default {
  name: 'MessageShowDialog',
  props: {
    visible: {
      required: true,
      type: Boolean
    },
    message: {
      required: true,
      type: Object
    }
  },
  model: {
    prop: 'visible',
    event: 'visible'
  },
  data () {
    return {
      queryLoading: false,
      dialogVisible: false,
      readOnly: true
    }
  },
  methods: {
    /**
     * 消费本条消息
     */
    consumeMessage () {
      if (this.message && this.message.id &&
        this.message['consumeFlag'] === BusinessConstants.MESSAGE_CONSUME_FALSE) {
        consumeMessageByIds([this.message.id])
      }
    },
    open () {
      this.consumeMessage()
    },
    /**
     * 关闭本对话框
     */
    closeDialog () {
      this.dialogVisible = false
    }
  },
  watch: {
    dialogVisible (val) {
      this.$emit('visible', val)
    },
    visible (val) {
      if (this.dialogVisible !== val) {
        this.dialogVisible = val
      }
    }
  }
}
</script>

<style scoped>

</style>
